package logic

import (
	"context"
	"getway/internal/svc"
	"getway/internal/types"
	"github.com/google/uuid"
	"goods/goods"
	"order/order"

	dtmgrpc "github.com/dtm-labs/client/dtmgrpc"
	"github.com/zeromicro/go-zero/core/logx"
)

type CreateOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
	return &CreateOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderRequest) (resp *types.CreateOrderResponse, err error) {
	saga := dtmgrpc.NewSagaGrpc("localhost:36790", uuid.NewString())
	saga.Add("192.168.137.192:8080/goods.Goods/UpdateGoodStock", "192.168.137.192:8080/goods.Goods/UpdateGoodStockRollback", &goods.UpdateGoodStockRequest{
		GoodID: 7,
		Stock:  -10,
	})
	saga.Add("192.168.137.192:8081/order.Order/CreateOrder", "192.168.137.192:8081/order.Order/CreateOrderRollback", &order.CreateOrderRequest{
		OrderNo: uuid.NewString(),
	})
	
	saga.TimeoutToFail = 10

	err = saga.Submit()
	if err != nil {
		return nil, err
	}

	return
}
